﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AppData.DTO;
using System.Drawing;
using System.Collections;
using AppData;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using System.Reflection;

namespace EbookReader.Components.Pages
{
    public partial class Main
    {
        //[Inject]
        //private IJSRuntime JSRuntime { get; set; }

        public List<Book> recommendedBooks;


        public Main()
        {
            GameDebug.Log($"Main Panel init");
        }

        protected override async Task OnInitializedAsync()
        {
            GameDebug.Log($"OnInitializedAsync -> GetRecommendedBooks");

            GetRecommendedBooks();

        }

        public async void GetRecommendedBooks()
        {
            var books = await RequestController.instance.GetReommendedBooks(10, (err) =>
                {
                    GameDebug.Log($"err:{err}");
                });

            GameDebug.Log($"GetReommendedBooks end: {books?.GetListStr()}");

            //InvokeAsync(() =>
            {
                this.recommendedBooks = books;
                StateHasChanged();

            }
            //).Wait();
            //await InvokeAsync(StateHasChanged);

            //JSRuntime.InvokeVoidAsync("location.reload");
        }

        public void OnClickBookItem(long id)
        {
            GameDebug.Log($"select item:{id}");

        }
        public string GetPdfSourcePath()
        {
            var name = "心灵能量.pdf";

            var assembly = Assembly.GetExecutingAssembly();

            string[] names = assembly
              .GetManifestResourceNames();

            GameDebug.Log($"names:{names.GetArrStr()}");

            string resourceName = names.Single(str => str.EndsWith(name));

            GameDebug.Log($"resourceName:{resourceName}");
            return resourceName;
        }
        public byte[] GetPdfSource()
        {
            var name = "心灵能量.pdf";

            var assembly = Assembly.GetExecutingAssembly();
            string resourceName = assembly
                .GetManifestResourceNames()
                .Single(str => str.EndsWith(name));

            GameDebug.Log($"resourceName:{resourceName}");
            byte[] bytes;
            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            {
                bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
            }

            var fileName = Path.Combine(Path.GetTempPath(), name);
            GameDebug.Log($"TempPath:{fileName}");
           // File.WriteAllBytes(fileName, bytes);

            return bytes;
        }

        public async Task UploadBook()
        {
            var res = await FilePicker.PickMultipleAsync();
            foreach (FileResult loc in res)
            {
                GameDebug.Log($"loc: {loc.FullPath}");
                //var result = await _Service.LoadCbzAsync(loc.FullPath);
                //await Task.Delay(2000);
                //foreach (var item in result)
                //{
                //    MainThread.BeginInvokeOnMainThread(() =>
                //    {
                //        Models.Add(item);
                //    });
                //}
            }
        }

        void TestUploadBook()
        {
            BookProto book = new BookProto();
            book.sub_genre = "";
            book.name = "心灵能量";
            book.description = "一本教会你心灵治愈的好书";
            book.author = "唐略";
            book.publisher = "云南人民出版社";
            book.publishDate = 20120201;

            var iconData = File.ReadAllBytes(testIconFile);
            using (MemoryStream stream = new MemoryStream(iconData))
            {
                Bitmap bitmap = new Bitmap(stream);
                var newBitmap = CommonFunctions.ScaleTexture(bitmap, 1024);
                book.tex = CommonFunctions.ConvertBitmapToJpegBytes(newBitmap);

                newBitmap.Dispose();
                //book.texWidth = bitmap.Width;
                //book.texHeight = bitmap.Height;
            }

            book.fileExt = Path.GetExtension(testBookFile);// ".pdf";
            book.contentData = System.IO.File.ReadAllBytes(testBookFile);

            GameDebug.Log($"BaseDirectory:{AppContext.BaseDirectory}");

            GameDebug.Log($"TestUploadBook book.fileExt:{book.fileExt}  ");

            //GameDebug.Log($"Resources:{Environment.GetFolderPath(Environment.SpecialFolder.Resources)}");
            RequestController.instance.UploadBook(book, (book) =>
            {
                GameDebug.Log($"TestUploadBook end: {book}  ");
            });

            //RequestController.instance.UploadBookWithStr(book, (book) =>
            //{
            //    GameDebug.Log($"TestUploadBook end: {book}");
            //});
        }

        static string testBookFile
        {
            get
            {
                return Path.Combine(AppContext.BaseDirectory, "../../../../../Resources/Test/心灵能量.pdf");
            }
        }
        static string testIconFile
        {
            get
            {
                return Path.Combine(AppContext.BaseDirectory, "../../../../../Resources/Test/心灵能量.png");
            }
        }
        static string testIconFile2
        {
            get
            {
                return Path.Combine(AppContext.BaseDirectory, "../../../../../Resources/Test/test.jpg");
            }
        }
    }
}
